/*
 * @Descripttion: 
 * @Author: yuxi
 * @Date: 2022-10-23 20:49:56
 * @LastEditors: yuxi
 * @LastEditTime: 2022-10-25 18:21:04
 */
import {
  Circle as CircleStyle, 
  Fill, 
  Stroke, 
  Style as BaseStyle, 
  Stroke as StrokeStyle, 
  Fill as FillStyle,
  Icon
} from 'ol/style';
import * as Size from 'ol/size';


class Style extends BaseStyle  {
   fromJSON(json) {
    if(json.stroke) {
      this.setStroke(new StrokeStyle({
        ...json.stroke
      }))
    }
    if(json.image) {
      // let size = undefined
      // if(json.image.size) {
      //   let s = json.image.size.split(',')
      //   size = new Size(parseInt(s[0]), parseInt(s[1]))
      // }
      this.setImage(new Icon({
        ...json.image,
        // size: size
      }))
    }
   }
   toJSON () {
    let json = {}
    let stroke = this.getStroke()
    if(stroke) {
      json['stroke'] = {
        color: stroke.getColor(),
        lineCap: stroke.getLineCap(),
        lineJoin: stroke.getLineJoin(),
        lineDash: stroke.getLineDash(),
        lineDashOffset: stroke.getLineDashOffset(),
        miterLimit: stroke.getMiterLimit(),
        width: stroke.getWidth(),
      }
    }

    let image = this.getImage()
    console.log('image is ', image)
    if(image) {
      json['image'] = {
        opacity: image.opacity_,
        anchor: image.anchor_,
        anchorXUnits: image.anchorXUnits_,
        anchorYUnits: image.anchorYUnits_,
        src: image.getSrc(),
        offset: image.offset_,
        offsetOrigin: image.offsetOrigin_,
        size: image.size_? image.size_.join(',') : '',
        scale: image.scale_

      }
    }
    return json
   }
   clone () {
    let geometry = this.getGeometry();
    if (geometry && typeof geometry === 'object') {
      geometry = /** @type {import("../geom/Geometry.js").default} */ (
        geometry
      ).clone();
    }
    return new Style({
      geometry: geometry,
      fill: this.getFill() ? this.getFill().clone() : undefined,
      image: this.getImage() ? this.getImage().clone() : undefined,
      renderer: this.getRenderer(),
      stroke: this.getStroke() ? this.getStroke().clone() : undefined,
      text: this.getText() ? this.getText().clone() : undefined,
      zIndex: this.getZIndex(),
    });
   }
}

export default Style;
